package com.example.ui.contact;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.ui.contact.adapter.AtMeAdapter;
import com.example.ui.contact.bean.MeItem;
import com.example.ui.contact.bean.MessageItem;
import com.example.ui.contact.chat.L;
import com.example.ui.contact.chat.PushMessageReceiver.EventHandler;
import com.example.ui.contact.db.MeMsgDB;
import com.example.ui.leftmenu.Item07View;
import com.example.util.BitmapLoad;
import com.example.util.Util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class AtMeActivity extends Activity implements EventHandler{

	private Button titleLeftBtn,titleRightBtn;	
	private TextView titleText;	
	private ListView listview;
	private LinearLayout noticeLayout;
	public static List<Map<String, Object>> listData;////MeItem
	private List<MeItem> msgList = new ArrayList<MeItem>();// 消息对象数组
	private MeMsgDB meMsgDB;
	private Intent intent;
	private String value;
	private Context mContext;
	private AtMeAdapter adapter;
	public static final int NEW_MESSAGE = 0x001;// 收到消息
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.chat_notice);
		intent=getIntent();
		value=intent.getStringExtra("text");
		meMsgDB = HSApplication.getInstance().getMeMsgDB();
		initMeMsgData();
		findView();
		listView();
		
		
	}
	

	private void findView() {
		titleLeftBtn = (Button)findViewById(R.id.layout_title_left_btn);
		titleRightBtn= (Button)findViewById(R.id.layout_title_right_btn);
		titleText=(TextView)findViewById(R.id.layout_title_middle_text);
		listview=(ListView)findViewById(R.id.chat_notice_listview);
		noticeLayout=(LinearLayout)findViewById(R.id.chat_notice_none_layout);
		
		initView();
	}

	public void initView(){
		titleLeftBtn.setText("返回");
		titleText.setText(value);
		titleRightBtn.setVisibility(View.INVISIBLE);
		titleLeftBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Item07View.my_adapter.notifyDataSetChanged();
				finish();
			}
		});
	}
	@SuppressWarnings("static-access")
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		if(keyCode== event.KEYCODE_BACK){
			Item07View.my_adapter.notifyDataSetChanged();
			finish();			
		}
		return true;
	}
	
	/**
	 * 从数据库中读出与我相关的历史数据
	 */
	public void initMeMsgData() {
		List<MeItem> list = meMsgDB.getMeMsgList();  //getMsg(mFromUser.getUserId(),MsgPagerNum);
		if (list.size() > 0) {
			for (MeItem entity : list) {

				msgList.add(entity);
			}
		}

	}
	private void listView(){
		getNoticeData();
		adapter = new AtMeAdapter(mContext,noticeLayout,msgList);
		listview.setAdapter(adapter);
		
	}

	/**
	 * 获得评论的数据
	 */
	private void getNoticeData() {
		listData=new ArrayList<Map<String,Object>>();
		//在这里获得评论的数据，然后添加到listData里面
		//、、、、、、、、、、、、、、、
	}
	
	

	class Holder {
//		ListView commentListView;
		ImageView headImg;
		TextView nameText;
		TextView title;//赞了我的:(这个字是不变的)
		TextView content;//评论内容
		
		TextView contentText;//原文内容
		ImageView contentImg1;//原文图片1
		ImageView contentImg2;//原文图片2
		ImageView contentImg3;//原文图片3

		TextView contentTime;//评论时间
	}

	/**
	 * 处理接收过来的消息
	 */
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			
			if (msg.what == NEW_MESSAGE) {
				String message = (String) msg.obj;
				if (message.length() > 0)
				{					
					MsgHandle(message);
				}		
				

			}
		}
	};

	
	public void MsgHandle(String message){
		try {

				
				String MeContent = new JSONObject(message.toString()).getString("Mention");
				JSONObject json = new JSONObject(MeContent);
				JSONObject share_json= json.getJSONObject("share");
				
				MeItem item = new MeItem();					
				item.setUserId(json.getString("uid"));
				item.setUserId2(share_json.getString("uid"));
				item.setType(share_json.getString("typeId"));
				item.setNick(share_json.getString("nickname"));
				item.setCommunityId(share_json.getString("communityid"));
				item.setTxt(share_json.getString("txt"));
				item.setDateLine(share_json.getString("dateline"));
				item.setLocation(share_json.getString("location"));					
				item.setName(json.getString("name"));
				item.setHeadPic(json.getString("headPic"));			
				HSApplication.getInstance().getMeMsgDB().saveMeMsg(item);	
				
				adapter.updateMsg(item);
				L.Debug("Mention run ok","");

		} catch (JSONException e) {
			e.printStackTrace();
		}		
	}
	@Override
	public void onMessage(String message) {
		try {
			String type = new JSONObject(message.toString()).getString("response").trim().toString();
			if(type.equals("Mention")){
				Message handlerMsg = handler.obtainMessage(NEW_MESSAGE);
				handlerMsg.obj = message;
				handler.sendMessage(handlerMsg);	
			}
		} catch (JSONException e) {
			L.Debug("AtMeActivity error", e.getMessage());
			e.printStackTrace();
		}

		
	}


	@Override
	public void onBind(String method, int errorCode, String content) {
		
	}


	@Override
	public void onNotify(String title, String content) {
		
	}


	@Override
	public void onNetChange(boolean isNetConnected) {
		
	}

	
	
	

}
